package pl.koziolekweb.testsonline.examples.console.controller;

import java.util.Collection;
import java.util.LinkedList;

import pl.koziolekweb.testsonline.examples.console.ConsoleResults;
import pl.koziolekweb.testsonline.examples.console.view.ViewGenerator;
import pl.koziolekweb.testsonline.model.Answer;
import pl.koziolekweb.testsonline.model.Question;
import pl.koziolekweb.testsonline.model.Test;
import pl.koziolekweb.testsonline.model.exam.Exam;
import pl.koziolekweb.testsonline.model.exam.Result;

public class Controller {

	private Test test;
	private Result result;
	private Exam exam;
	private ViewGenerator viewGenerator;

	public Controller(Result result, Test test, Exam exam) {
		this.result = result;
		this.test = test;
		this.exam = exam;
		this.viewGenerator = new ViewGenerator();
	}

	public void start() {
		String examId = exam.getId();
		viewGenerator.generate("Rozpoczynamy test:\n" + examId);
		Collection<Question> questions = test.getQuestions();
		for(Question question : questions){
			viewGenerator.generate(question.getContent());
			LinkedList<Answer> answers = (LinkedList<Answer>) question.getAnswers();
			int i = 1;
			for(Answer answer : answers){
				viewGenerator.generate(i+". "+answer.getContent());
				i++;
			}
			Integer response = new Integer(viewGenerator.read(""));
			if(answers.get(response-1).getIsGood()){
				((ConsoleResults)this.result).addPoint();
			}
			else{
			}
		}
	}

}
